﻿#region File Description
/****************************************************************************
*                                                                           *
* Game Manager.                                                             *
*                                                                           *
* Copyright (c) Ionescu Marius. All rights reserved(2013).                  *
*                                                                           *
****************************************************************************/
#endregion
namespace Silverlight3D
{
    using System.Windows.Shapes;
    using System.Windows.Media;
    using System;
    using System.Windows;
    using System.Windows.Controls;

    public class VectorLine : VectorObject
    {
        public Vector Start { get; set; }
        public Vector End { get; set; }

        public override double GetMaxZAfterRotation(Projector projector, Vector rotationCenter)
        {
            return Math.Max(projector.Rotate(this.Start, rotationCenter).Z, projector.Rotate(this.End, rotationCenter).Z);
        }

        public override UIElement Project(Projector projector, IShader shader,  Vector rotationCenter)
        {
            return this.Project(projector, shader, projector.ViewPosition, rotationCenter);
        }

        public override UIElement Project(Projector projector, IShader shader, Vector viewPosition, Vector rotationCenter)
        {
            var line = new Line();

            var start = projector.Project(this.Start, viewPosition, rotationCenter);
            var end = projector.Project(this.End, viewPosition, rotationCenter);

            if (start == null || end == null)
            {
                return null;
            }

            line.X1 = start.Value.X;
            line.Y1 = start.Value.Y;

            line.X2 = end.Value.X;
            line.Y2 = end.Value.Y;

            double brightness = this.Shading(projector);

            line.Stroke = shader.GetShadedBrush(this.StrokeColor, brightness);
            line.StrokeThickness = this.StrokeThickness;

            line.UseLayoutRounding = true;

            return line;
        }

        public override Vector Center()
        {
            return (this.Start + this.End) * 0.5;
        }

        private double Shading(Projector projector)
        {
            var distance = this.CachedMaxZAfterRotation - projector.ViewPosition.Z;

            double result = 1 - distance / 2000;

            if (result < 0)
            {
                result = 0;
            }

            result = (1 - this.BrightnessFactor) * result + this.BrightnessFactor;

            if (result > 1)
            {
                result = 1;
            }

            return result;
        }

        public override VectorObject Clone()
        {
            var result = new VectorLine();

            result.Start = this.Start.Clone();
            result.End = this.Start.Clone();
            result.StrokeColor = this.StrokeColor;
            result.StrokeThickness = this.StrokeThickness;

            return result;
        }
    }
}